<?php
function writeShoppingCart($lang) {
	$cart = $_SESSION['cart'];
	if (!$cart) {
		return '<p>'.$lang['CONTENT_CART_NO_ITEM_1'].'<span class="tahoma12CK"><strong>'.$lang['CONTENT_CART_NO_ITEM_2'].' </strong></span>'.$lang['CONTENT_CART_NO_ITEM_3'].'</p>';
	} else {
		// Parse the cart session variable
		$items = explode(',',$cart);
		//$s = (count($items) > 1) ? 's':'';
		return $lang['CONTENT_CART_NO_ITEM_1.1'].'<span class="tahoma12CK"><strong>'.count($items).$lang['CONTENT_CART_NO_ITEM_2.1'].'</strong></span> '.$lang['CONTENT_CART_NO_ITEM_3'];
	}
}

function showCart($lang) {
	global $db;
	$cart = $_SESSION['cart'];
	$location = $_SESSION['locationPrice'] ;
	if($location == null){
		if($_SESSION['isAhli'] == 'yes' ){ 
			$priceLocation = 'price_wm_ahli';
		}else{ 
			$priceLocation = 'price_wm';
		}
	}else{
		if($location == 1){
			if($_SESSION['isAhli'] == 'yes' ){ 
				$priceLocation = 'price_em_ahli';
			}else{ 
				$priceLocation = 'price_em';
			}
		}else{
			if($_SESSION['isAhli'] == 'yes' ){ 
				$priceLocation = 'price_wm_ahli';
			}else{ 
				$priceLocation = 'price_wm';
			}
		}
	}
	
	
	if ($cart) {
		$items = explode(',',$cart);
		$contents = array();
		foreach ($items as $item) {
			$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
		}
		$output[] = '<div id="cartDiv" ><form action="cart.php?action=update" method="post" id="cart">';
		$output[] = '<table> ';
		foreach ($contents as $id=>$qty) {
			$sql = 'SELECT * FROM item WHERE id = '.$id;
			$rCheck= $db->sql_total($sql);
			$rItem= $db->sql_list($sql);
			if($rCheck <> 0){
			$output[] = '<tr>';
			$output[] = '<td><a href="cart.php?action=delete&id='.$id.'" class="r">Remove</a></td>';
			$output[] = '<td> Name : '.$rItem['name'].'</td>';
			$output[] = '<td> Price RM'.$rItem[$priceLocation].'</td>';
			$output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
			$output[] = '<td>RM '.($rItem[$priceLocation] * $qty).'</td>';
			$total += $rItem[$priceLocation] * $qty;
			$output[] = '</tr>';
			}
		}
		$output[] = '</table>';
		$output[] = '<p>Grand total: <strong>RM '.$total.'</strong></p>';
		$output[] = '<div><button type="submit">Update cart</button></div>';
		$output[] = '</form> </div>';
	} else {
		$output[] = '<p>You shopping cart is empty.</p>';
	}
	return join('',$output);
}
?>
